package com.docking.heylo.service.impl;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.domain.AlipayTradeAppPayModel;
import com.alipay.api.internal.util.AlipaySignature;
import com.alipay.api.request.AlipayTradeAppPayRequest;
import com.alipay.api.request.AlipayTradeRefundRequest;
import com.alipay.api.response.AlipayTradeAppPayResponse;
import com.alipay.api.response.AlipayTradeRefundResponse;
import com.docking.heylo.pojo.PayOrderPackage;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.Map;

@Component
@Configuration
public class AliPayServerImpl {

    /***
     * 程序ID
     */
    private final String APP_ID = "2021001186684295";

    /**
     *APP 公开密钥
     */
    private final String AliPay_Public_Key = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAorX5Sftw3PIxR+5PHAgjUheKW2WKE47UV7X+Fh6GnT+LY237ByiH+cPcpm46NTHU3/vkwtbJDTgA9liFx7kI5vbpw+UdUMsgv5/BJlEHhxLsrC6lHR0DrhHyGbP0MvyFlsaprqTl6rJulEcuNTELdLa+Q37WxNJkETo+hZWUbFR43AsXjYlIzdOb9J0c+OO+3C5yIxzvlkEecpbtIA+gEp2nXRmQKz1BMAEHdx3/ovin+0cqVOcuf3Ntw0LuWY3NW4aeA/caTHndmJkzzsKYQlKiwLMjZTbdMHhHBjLmD3BCZ/XzBwNpakyBI+JfC2E5Im/AtHFmqELxtVMK8jOz2QIDAQAB";

    /***
     * APP 私密密钥
     */
    private final String AliPay_Private_Key = "MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQCt8+XjGeRjCWDJyzidRKT8/oAqZimfAqAma9Dj/4aquVmAf2JNXLtLFjeU4dq0JfxHJHjIN1JtBwENFZrtWrFRGm+/AvdLwjoyEXYm9WWx6b2PUh4N1N9ItuyGi/umzba0oEaOHzZzSf6+5Tse+MITVtH6x7mO81xfIXL5xZcDQ3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8eiXORPUCAP08b168XqpXCYWOflrOD4kAgHRPkLpvouf6ePMmWA5+YnL8pcKVKYm09S+GhGlRP0qUSv1caTMe0fiJX2DeXbmoYMUHCPE3fUC41YtZcsmML/PdlEkbTMwUfCxFZilKWUp5pCHISLqDE5caUbQKBgGrPlyGkTI6tlUteniql14lfyka+1fQoY85oOtVoqdMDRV45x3mGQs+1cfru5mOrtorQ6Lv6p0sIOxxwaIRdCTsjQjwSurwjFAPr1tvk5c3TkQUO6Tx351Yr9uL8oY3AfqWPEHUUyYRt3tDEtju8zkbDspCHxaWbwqwq6YWh0TKm";


    private final AlipayClient alipayClient = new DefaultAlipayClient("https://openapi.alipay.com/gateway.do" ,
            APP_ID, AliPay_Private_Key,"json", "UTF-8", AliPay_Public_Key, "RSA2");


    public AlipayTradeAppPayResponse sendPay(PayOrderPackage order) throws AlipayApiException {

        //实例化具体API对应的request类,类名称和接口名称对应,当前调用接口名称：alipay.trade.app.pay
        AlipayTradeAppPayRequest request = new AlipayTradeAppPayRequest();

        //SDK已经封装掉了公共参数，这里只需要传入业务参数。以下方法为sdk的model入参方式(model和biz_content同时存在的情况下取biz_content)。
        AlipayTradeAppPayModel model = new AlipayTradeAppPayModel();
        model.setBody(order.getBody());
        model.setSubject(order.getSubject());
        model.setOutTradeNo(order.getTradeNo());                                        //订单号
        model.setTimeoutExpress("30m");

        //测试支付0.01
        model.setTotalAmount("0.01");
//        model.setTotalAmount(order.getAmount().toString());

        model.setProductCode("QUICK_MSECURITY_PAY");
        request.setBizModel(model);
        request.setNotifyUrl(order.getNotifyUrl());//回调地址

        //这里和普通的接口调用不同，使用的是sdkExecute
        AlipayTradeAppPayResponse response = alipayClient.sdkExecute(request);
        return response;
    }




























    /***
     * 对返回参数进行RSA签名校验
     *
     * @param params
     * @return
     */
    public boolean rsaCheckV1(Map<String,String> params){
        String signType = params.get("sign_type");
        try {
            return AlipaySignature.rsaCheckV1(params,AliPay_Public_Key , "UTF-8",signType);
        } catch (AlipayApiException e) {
            e.printStackTrace();
        }
        return false;
    }



    public AlipayTradeRefundResponse getRefundResponse(PayOrderPackage order)throws AlipayApiException{
        if(order == null){
            throw new NullPointerException("找不到该订单");
        }
        BigDecimal realOrderPrice = order.getAmount();
//        realOrderPrice = new BigDecimal("0.01");

        AlipayTradeRefundRequest request = new AlipayTradeRefundRequest();
        request.setBizContent("{" +
                "\"out_trade_no\":\""+order.getTradeNo()+"\"," +
                "\"refund_amount\":\""+ realOrderPrice.toString() +"\"," +
                "\"refund_reason\":\"正常退款\"" +
                "}");
        AlipayTradeRefundResponse response = alipayClient.execute(request);

        return response;
    }







}
